package com.yyt.elasticsearch.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestClientBuilder.RequestConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

@Configuration
@Slf4j
public class ElasticSearchConfiguration {

    /**
     * elk集群地址
     */
    @Value("${elasticsearch.ip}")
    private  String ip;
    /**
     * 端口
     */
    @Value("${elasticsearch.port}")
    private int port;
    /**
     * 集群名称
     */
    @Value("${elasticsearch.cluster.name}")
    private String clusterName;

    /**
     * 连接池
     */
    @Value("${elasticsearch.pool}")
    private String poolSize;
    @Value("${elasticsearch.schema}")
    private String schema;
    @Value("${elasticsearch.connectTimeOut}")
    private int connectTimeOut;
    @Value("${elasticsearch.socketTimeOut}")
    private int socketTimeOut;
    @Value("${elasticsearch.connectionRequestTimeOut}")
    private int connectionRequestTimeOut;
    @Value("${elasticsearch.maxConnectNum}")
    private int maxConnectNum;
    @Value("${elasticsearch.maxConnectPerRoute}")
    private int maxConnectPerRoute;
    private static ArrayList<HttpHost> hostList = null;


    private void constructHttpHostParams() {
        // log.info("ips={},port={},schema={},connectTimeOut={},socketTimeOut={}.", ip, port, schema, connectTimeOut, socketTimeOut);
        hostList = new ArrayList<>();
        String[] ipStrs = ip.split(",");
        for (String ip : ipStrs) {
            hostList.add(new HttpHost(ip, port, schema));
        }
    }

    @Bean
    public RestHighLevelClient client() {
        // log.info("start connect elasticsearch");
        constructHttpHostParams();
        RestClientBuilder builder = RestClient.builder(hostList.toArray(new HttpHost[hostList.size()]));
//        builder.setMaxRetryTimeoutMillis(5 * 60 * 1000);
        // 异步httpclient连接延时配置
        builder.setRequestConfigCallback(new RequestConfigCallback() {
            @Override
            public Builder customizeRequestConfig(Builder requestConfigBuilder) {
                requestConfigBuilder.setConnectTimeout(connectTimeOut);
                requestConfigBuilder.setSocketTimeout(socketTimeOut);
                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);

                return requestConfigBuilder;
            }
        });
        // 异步httpclient连接数配置
        builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.setMaxConnTotal(maxConnectNum);
                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
                return httpClientBuilder;
            }
        });
        RestHighLevelClient client = new RestHighLevelClient(builder);
        // log.info("cluster info:" + client.cluster().toString());
        return client;
    }

}

